﻿using System;

using zijian666.Converts.Abstractions;
using zijian666.Converts.Convertor;
using zijian666.Converts.Core;

namespace zijian666.Converts.Factory;

/// <summary>
/// 可空值类型转换器 <seealso cref="NullableConvertor{TValue}"/> 工厂
/// </summary>
public sealed class NullableConvertorFactory : IConvertorFactory
{

    /// <inheritdoc/>
    public MatchedConvertor<T> Create<T>()
    {
        var type = typeof(T);
        if (Nullable.GetUnderlyingType(type) is not null)
        {
            var conv = (IConvertor<T>)Activator.CreateInstance(typeof(NullableConvertor<>).MakeGenericType(Nullable.GetUnderlyingType(type)));
            return new MatchedConvertor<T>(conv, MacthedType.Equals);
        }
        return default;
    }
}
